import os
import fnmatch
import tables as tb
from records import recordtype 
import matplotlib.pylab as plt

dir_files = recordtype("dir_files", ["hdf5File", "xmlFile"])

def locateFirst(pattern, root=os.curdir):
    '''Locate all files matching supplied filename pattern in and below
    supplied root directory.'''
    for path, dirs, files in os.walk(os.path.abspath(root)):
        for filename in fnmatch.filter(files, pattern):
            return os.path.join(path, filename)

def get_sim_files(root_dir):
    sim_files = []
    for root, dirs, files in os.walk(root_dir):        
        for name in files:
            if (name == "MeasureTemp.h5"):
                hdf5FileName = os.path.join(root, name)
                xmlFileName = os.path.join(root, "sim.xml")
                if (tb.isHDF5File(hdf5FileName)): 
                    sim_files.append({"hdf5File":hdf5FileName, "xmlFile":xmlFileName})                 
    return sim_files
def errobarXY(x, y, yerr, file_name):
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.hold(True)
        if (isinstance(y, list)):
            for cy, cyerr in zip(y, yerr): 
                ax.errorbar(x, cy, cyerr)
        else:
            ax.errorbar(x, y, yerr)
        fig.savefig(file_name)
        fig.clf() 
        
MaxEntAlgData = recordtype('MaxEntAlgData', \
                                ['dir', \
				'dump_dir', \
                                 'zeroFreq',\
				 'zeroFreqVal',\
                                 'zeroFreqVar',\
				 'omegaMax', \
                                 'numOfOmega', \
                                 'obs', \
                                 'moment', \
                                 'alphaMin', \
                                 'alphaMax', \
                                 'alphaHigh', \
                                 'numAlpha', \
                                 'errorLevel', \
                                 'beta', \
                                 'G',\
                                 'MU',\
                                 'gc',\
                                 'muc',\
                                 'nu',\
                                 'omegaCutOff', \
                                 'omegaCutOffHigh', \
                                 'deltaOmegaBin', \
                                 'omegaMatP', \
                                 'omegaMatPReBin', \
                                 'omegaP', \
                                 'svdCutOff', \
                                 'momentMes', \
				 'errorIntervals', \
				 'deltapos',\
                                 'momentMesBins'], default=0)
BraynData = recordtype('BraynData', \
                                ['U', \
                                 'V', \
                                 'Sigma', \
                                 'data', \
                                 'data'\
                                 'md'\
                                 ], default=0)
SimData = recordtype('SimData', \
                                ['kp', \
                                 'datap', \
                                 ], default=0)
BraynResult = recordtype('BraynResult', \
                                ['f', \
                                 'entropy', \
                                 'chisqr', \
                                 'Q', \
				 'nabQ', \
                                 'alpha', \
                                 'probAlpha', \
                                 'logPart', \
                                 'ng',\
				 'diff'\
                                 ], default=0)
LapResult = recordtype('LapResult', \
                                ['f', \
                                 'smooth', \
                                 'chisqr', \
                                 'alpha', \
                                 ], default=0)
